<?php

use App\Models\User;
use Illuminate\Http\Request;

use function Livewire\Volt\mount;
use function Livewire\Volt\state;
use function Livewire\Volt\layout;
use function Livewire\Volt\with;

layout('components.layouts.app');


state([
    'search' => '',
    'perPage' => 1,
    'currentPage' => 1,
]);

with(fn() => [
    'users' => User::query()
        ->when($this->search, function($query, $search) {
            $query->where(function($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                  ->orWhere('email', 'like', "%{$search}%");
            });
        })
        ->paginate($this->perPage, ['*'], 'page', $this->currentPage)
]);

mount(function (Request $request) {
    if ($request->wantsJson()) {
        echo json_encode($this->users);
        exit;
    }
});

$setPage = function ($currentPage) {
    $this->currentPage = $currentPage;
};
?>

<div class="flex flex-col gap-4 p-4 pb-32 min-h-screen">
    <h1 class="text-2xl font-bold mb-4">{{ __('User Management') }}</h1>

    <!-- 搜索框 -->
    <div class="mb-4">
        <input
            type="text"
            wire:model.live.debounce.300ms="search"
            placeholder="{{ __('Search users...') }}"
            class="w-full md:w-1/3 px-4 py-2 border border-neutral-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
        >
    </div>

    <div class="overflow-hidden rounded-xl border border-neutral-200 mb-8">
        <table class="min-w-full divide-y divide-neutral-200">
            <thead class="bg-neutral-50">
                <tr>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-neutral-500 uppercase tracking-wider">ID</th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-neutral-500 uppercase tracking-wider">{{ __('Name') }}</th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-neutral-500 uppercase tracking-wider">{{ __('Email') }}</th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-neutral-500 uppercase tracking-wider">{{ __('Created At') }}</th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-neutral-500 uppercase tracking-wider">{{ __('Verified') }}</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-neutral-200">
                @forelse ($users as $user)
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-neutral-500">{{ $user->id }}</td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="flex items-center">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-neutral-100 flex items-center justify-center">
                                    <span class="text-neutral-600 font-medium">{{ $user->initials() }}</span>
                                </div>
                                <div class="ml-4">
                                    <div class="text-sm font-medium text-neutral-900">{{ $user->name }}</div>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-neutral-500">{{ $user->email }}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-neutral-500">{{ $user->created_at->format('Y-m-d') }}</td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            @if ($user->email_verified_at)
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">{{ __('Verified') }}</span>
                            @else
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-neutral-100 text-neutral-800">{{ __('Unverified') }}</span>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="5" class="px-6 py-4 whitespace-nowrap text-sm text-neutral-500 text-center">{{ __('No users found') }}</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <!-- 使用正确的分页控件 -->
    <div class="fixed bottom-0 left-0 right-0 bg-white shadow-lg py-6 border-t border-gray-200">
        <div class="flex flex-col items-center">
            <nav aria-label="Pagination" class="flex items-center">
                <!-- 上一页按钮 -->
                <button
                    wire:click="setPage({{ $currentPage > 1 ? $currentPage - 1 : 1 }})"
                    class="px-6 py-3 mx-1 text-base rounded-lg font-medium border shadow-sm bg-white text-gray-700 border-gray-300 hover:bg-gray-100"
                    {{ $currentPage <= 1 ? 'disabled' : '' }}
                >
                    &laquo; 上一页
                </button>

                <!-- 页码按钮 -->
                @for ($i = 1; $i <= $users->lastPage(); $i++)
                    <button
                        wire:click="setPage({{ $i }})"
                        class="px-6 py-3 mx-1 text-base rounded-lg font-medium border shadow-sm {{ $i == $currentPage ? 'bg-blue-600 text-white border-blue-700' : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-100' }}"
                    >
                        {{ $i }}
                    </button>
                @endfor

                <!-- 下一页按钮 -->
                <button
                    wire:click="setPage({{ $currentPage < $users->lastPage() ? $currentPage + 1 : $users->lastPage() }})"
                    class="px-6 py-3 mx-1 text-base rounded-lg font-medium border shadow-sm bg-white text-gray-700 border-gray-300 hover:bg-gray-100"
                    {{ $currentPage >= $users->lastPage() ? 'disabled' : '' }}
                >
                    下一页 &raquo;
                </button>
            </nav>
            <div class="text-center mt-2 text-gray-500">
                <span>共 {{ $users->total() }} 条记录，当前第 {{ $currentPage }}/{{ $users->lastPage() }} 页</span>
            </div>
        </div>
    </div>
</div>
